<?php
namespace  Home\Biz;
use Home\Biz\CommonBiz;
class RmaBiz {
	public static function generateOrderRmaId() {
		$seq = 100000000 + CommonBiz::getNewSeq ( 'orma' );
		return 'RMA' . $seq;
	}
	public static function generateOrderRmaDetailId() {
		$seq = 100000000 + CommonBiz::getNewSeq ( 'ormad' );
		return 'RMAD' . $seq;
	}
	public static function caculateTotalPage($totalCount, $pageSize) {
		$count = intval ( $totalCount / $pageSize );
		
		$countLeft = $totalCount % $pageSize;
		
		if ($countLeft != 0) {
			$count = $count + 1;
		}
		
		return $count;
	}
	public static function validateRma($rma_en) {
	}
	public static function addRma($rma_en) {
	}
	public static function addRmaDetail($rma_id, $d_rma_en) {
	}
	public static function updateRma($rma_en) {
	}
	
	
	public  static function  queryOrderDetailByPara($paras_en){
		
		$condition_array = array();
		
		if(!empty($paras_en['order_id'])){
			array_push($condition_array, "o.order_id='".$paras_en['order_id']."'");
		}
		if(!empty($paras_en['dealer_id'])){
			array_push($condition_array, "o.buyer_alipay_no='".$paras_en['dealer_id']."'");
		}
		
		array_push($condition_array, "o.order_type = 8");
		array_push($condition_array, "o.order_status IN(31,33)");
		array_push($condition_array, "od.qty = od.ship_qty");
		array_push($condition_array, "(od.ship_qty != od.rma_qty or od.rma_qty IS NULL)");
		$condition_sql = implode(' AND ', $condition_array);
		
		$order_detail_lst = M('order_info','','DB_whaley_trade')->alias('o')->where($condition_sql)->
		Field("oid,buyer_alipay_no,sku_id,sku_name,qty,rma_qty ")->
		join("order_detail  od ON o.order_id = od.order_id")->select();
		
//  		$res=  M('','','DB_whaley_trade')->getLastSql();		
//  		return $res."345";
		foreach ($order_detail_lst as &$order_detail ){
			if (empty ( $order_detail ['rma_qty'] )) {
			
				$order_detail ['rma_qty'] = 0;
			}
			$order_detail['left_rma_qty'] = $order_detail['qty']-$order_detail['rma_qty'];
		}
		
		
		return $order_detail_lst;
	}
	
	public  static  function  queryProductBySkuPara($paras_en){
		$condition_array = array();
		if(!empty($paras_en['sku_id'])){
			array_push($condition_array, "s.sku_id='".$paras_en['sku_id']."'");
		}
		$condition_sql = implode(' AND ', $condition_array);
		$product_en=M('sku','','DB_whaley_product')->alias('s')->where($condition_sql)->
		Field("s.sku_id,s.sku_name,p.product_id, p.product_name, p.material_no ,p.material_desc")->
		join("product p ON  s.product_id = p.product_id")->find();
		return $product_en;
		
	}
	
}